<template>
  <div>
    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
      <el-form-item label="编号" prop="name">
        <el-input v-model="ruleForm.id" readOnly></el-input>
      </el-form-item>
      <el-form-item label="名称" prop="name">
        <el-input v-model="ruleForm.name"></el-input>
      </el-form-item>
      <el-form-item label="作者" prop="author">
        <el-input v-model="ruleForm.author"></el-input>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')">修改</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
  const axios = require('axios')
  export default {
    data() {
      return {
        ruleForm: {
          name: '',
          author: ''
        },
        rules: {
          name: [
            { required: true, message: '请输入图书名称', trigger: 'blur' },
          ],
          author: [
            { required: true, message: '请输入作者名称', trigger: 'blur' },
          ]
        }
      };
    },
    methods: {
      submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            const _this = this
            axios.put('http://localhost:9999/update',this.ruleForm).then(function (resp) {
              if (resp.data=='success'){
                _this.$alert(_this.ruleForm.name+'修改成功', '恭喜你，成功了', {
                  confirmButtonText: '确定',
                  callback: action => {
                    _this.$router.push('/BookManage')
                  }
                });
              }
            })
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },
      resetForm(formName) {
        this.$refs[formName].resetFields();
      }
    },
    created() {
      const _this = this
      // alert(this.$route.query.id)
      axios.get("http://localhost:9999/findById/"+this.$route.query.id).then(
        function(resp){
          _this.ruleForm = resp.data
        }
      )
    }

  }
</script>
